package com.loren.demo07.cluster.controller;

import com.loren.demo07.cluster.dto.ClusterMessage;
import com.loren.demo07.cluster.websocket.service.ClusterService;
import lombok.AllArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
public class ClusterController {

    private final Environment environment;

    private final ClusterService clusterService;

    @PostMapping("/to-user")
    public String sendToUser(@Validated @RequestBody ClusterMessage message) {
        this.clusterService.sendTextMessage(message);
        return "success, port = " + this.environment.getProperty("server.port");
    }

    @PostMapping("/to-group")
    public String sendToGroup(@Validated @RequestBody ClusterMessage message) {
        this.clusterService.sendTextMessage(message);
        return "success, port = " + this.environment.getProperty("server.port");
    }
}
